using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Collections.Generic;

/// <summary>
/// Summary description for CheckPhoneNumber
/// </summary>
public static class CheckPhoneNumber
{
    public static string GetCountryFromCode(string code)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("RO", "Romania");

        dict.Add("HU", "Ungaria");
        dict.Add("GB", "Marea Britanie");
        dict.Add("F", "Franta");
        dict.Add("PL", "Polonia");
        dict.Add("Elvetia", "Elvetia");
        dict.Add("Olanda", "Olanda");
        dict.Add("Spania", "Spania");
        dict.Add("Portugalia", "Portugalia");
        dict.Add("SUA", "SUA");

        return dict[code];
    }

    public static string GetCountry(string phone)
    {
        string returnPhone = phone;
        string country = "";
        
        //keep only numbers
        returnPhone = Regex.Replace(returnPhone, "[^0-9]", "");

        //delete first zeros
        returnPhone = Regex.Replace(returnPhone, "^0+", "");

        //get first number
        MatchCollection matches = Regex.Matches(returnPhone, "^\\d");

        if (matches.Count > 0)
        {
            string first = matches[0].Value.ToString();
            if (first == "1")
                country = "SUA";
            else
                country = "";
        }

        //get first two numbers
        matches = Regex.Matches(returnPhone, "^\\d\\d");

        if (matches.Count > 0)
        {
            string firstTwo = matches[0].Value.ToString();

            switch (firstTwo)
            {
                case "40":
                    country = "RO";
                    break;
                case "36":
                    country = "HU";
                    break;
                case "39":
                    country = "I";
                    break;
                case "49":
                    country = "D";
                    break;
                case "43":
                    country = "A";
                    break;
                case "41":
                    country = "Elvetia";
                    break;
                case "44":
                    country = "GB";
                    break;
                case "33":
                    country = "F";
                    break;
                case "48":
                    country = "PL";
                    break;
                case "31":
                    country = "Olanda";
                    break;
                case "32":
                    country = "Belgia";
                    break;
                case "34":
                    country = "Spania";
                    break;
                case "91":
                    country = "IND";
                    break;
                case "61":
                    country = "AUS";
                    break;
                case "27":
                    country = "ZA";
                    break;
                default:
                    break;
            }
        }

        //get first three numbers
        matches = Regex.Matches(returnPhone, "^\\d\\d\\d");
        if (matches.Count > 0)
        {
            string firstThree = matches[0].Value.ToString();

            switch (firstThree)
            {
                case "358":
                    country = "Finlanda";
                    break;
                case "351":
                    country = "Portugalia";
                    break;
                default:
                    break;
            }
        }

        return country;
    }
    
    public static int[] IsOkNumber(string number)
    {
        string country = GetCountry(number);
        int[] isOk = new int[2];

        if (country != "")
        {
            switch (country)
            {
                case "RO":
                    isOk = CheckRomanianNumber(number);
                    break;
                case "HU":
                    isOk = CheckHungarianNumber(number);
                    break;
                case "I":
                    isOk = CheckItalianNumber(number);
                    break;
                case "D":
                    isOk = CheckGermanNumber(number);
                    break;
                case "A":
                    isOk = CheckAustriaNumber(number);
                    break;
                case "Elvetia":
                    isOk = CheckSwitzerlandNumber(number);
                    break;
                case "GB":
                    isOk = CheckUKNumber(number);
                    break;
                case "F":
                    isOk = CheckFranceNumber(number);
                    break;
                case "PL":
                    isOk = CheckPolandNumber(number);
                    break;
                case "Olanda":
                    isOk = CheckNetherlandsNumber(number);
                    break;
                case "Belgia":
                    isOk = CheckBelgiumNumber(number);
                    break;
                case "Spania":
                    isOk = CheckSpainNumber(number); string fff=null;
                    break;
                case "IND":
                    isOk = CheckIndiaNumber(number);
                    break;
                case "AUS":
                    isOk = CheckAustraliaNumber(number);
                    break;
                case "ZA":
                    isOk = CheckSouthAfricaNumber(number);
                    break;
                case "Finlanda":
                    isOk = CheckFinlandNumber(number);
                    break;
                case "Portugalia":
                    isOk = CheckPortugalNumber(number);
                    break;
                case "NorthAmerica":
                    isOk = CheckNorthAmericaNumber(number);
                    break;
                default:
                    break; 
            }
        }
        return isOk;
    }

    private static int[] CheckRomanianNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^00", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^40([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^40([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((71)|(72)|(73)|(74)|(75)|(76)|(77)|(78))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length < 8 || phoneWithoutCC.Length > 9)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile};

        return returnValue;
    }

    private static int[] CheckHungarianNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^00", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^(?:(?:36)|(?:06))([0-9]{8,9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^(?:(?:36)|(?:06))([0-9]{8,9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((20)|(30)|(70)|(90)|(91))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length < 8 || phoneWithoutCC.Length > 9)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckItalianNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^39([0-9]{8,12})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^39([0-9]{8,12})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^3[0-9]{9}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 10 || phoneWithoutCC.Length < 7)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckGermanNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^49([0-9]{5,14})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^49([0-9]{5,14})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((15)|(16)|(17))[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 14 || phoneWithoutCC.Length < 5)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckAustriaNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^43([0-9]{5,13})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^43([0-9]{5,13})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((650)|(660)|(664)|(676)|(680)|(681)|(688)|(699))[0-9]{7,8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 13 || phoneWithoutCC.Length < 5)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckSwitzerlandNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^41([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^41([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((74)|(76)|(77)|(78)|(79))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckUKNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^44([0-9]{9,10})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^44([0-9]{9,10})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((74)|(75)|(7624)|(77)|(78)|(79))[0-9]{6,8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 10 || phoneWithoutCC.Length < 9)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckFranceNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^33([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^33([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((6)|(7))[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckPolandNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^48([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^48([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((5)|(6)|(7)|(8))[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckNetherlandsNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^31([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^31([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^(6)[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckBelgiumNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^32([0-9]{8,9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^32([0-9]{8,9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((47)|(48)|(49))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckSpainNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^34([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^34([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((6[1-9])|(7[1-9]))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8 || phoneWithoutCC.StartsWith("60"))
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckIndiaNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^91([0-9]{10})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^91([0-9]{10})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((9)|(8)|(7))[0-9]{9}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 10 || phoneWithoutCC.Length < 9)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckAustraliaNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^61([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^61([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^(4)[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckSouthAfricaNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^27([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^27([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((71)|(72)|(73)|(74)|(76)|(78)|(79)|(82)|(83)|(84)|(85))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckFinlandNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^358([0-9]{8,9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^358([0-9]{8,9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^((4[0-9])|(50))[0-9]{7}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 7)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckPortugalNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^351([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^351([0-9]{9})", "$1");

        //check if is mobile phone
        MatchCollection matches = Regex.Matches(phoneWithoutCC, "^(9)[0-9]{8}");
        if (matches.Count > 0)
            isMobile = 1;

        if (phoneWithoutCC.Length > 9 || phoneWithoutCC.Length < 8)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }

    private static int[] CheckNorthAmericaNumber(string number)
    {
        string phone = number;
        string phoneWithoutCC = "";
        int isNumber = 0;
        int isMobile = 0;

        phone = Regex.Replace(phone, "[^0-9]", "");
        phone = Regex.Replace(phone, "^0+", "");

        //delete country code
        MatchCollection matchesCC = Regex.Matches(phone, "^1[2-9]([0-9]{9})");
        if (matchesCC.Count > 0)
            phoneWithoutCC = Regex.Replace(phone, "^1[2-9]([0-9]{9})", "$1");

        //check if is mobile phone
        //normal phones and mobile phones don't have separate formats, prefixes in NANP

        if (phoneWithoutCC.Length > 10 || phoneWithoutCC.Length < 9)
            isNumber = 0;
        else
            isNumber = 1;

        int[] returnValue = new int[2] { isNumber, isMobile };

        return returnValue;
    }
}
